
import {ref} from 'vue'
import userInfo from '@/types/userInfo'
import request from '@/base/request'
import { ElMessage } from 'element-plus'
import Page from '@/base/page'
import { number } from 'echarts'
import Role from '@/types/role'
export default function(){



    interface RoleList{
        raminRoleList:Array<Role>,  //没有的角色
        userRoleList:Array<Role>,   //已经有的角色
    }

    interface Disable{
        userId?:number,
        reason?:string,
        d:number,
        h:number,
        m:number
    }

    const disableParm=ref<Disable>({
        d:0,
        h:0,
        m:0
    })


    const userParm=ref({
        userId:'',
        userName:'',
        pageNo:1,
        pageSize:10,
    })

    const roleList=ref<RoleList>()

    const addRoleList=ref({
        userId:-1,
        chooseroleList:[]
    })


    const userList=ref<Page<userInfo>>()


    async function diasbleUser() {
        const response=(await request.post(`/user/admin/disable`,disableParm.value)).data
        if(response.code===200){
            ElMessage.success("禁封成功")
        }else{
            ElMessage.error(`失败:${response.msg}`)
        }
    }

    async function getUserRoleList(userId:number) {
        const response=(await request.get(`/user/admin/getRoleListByid/${userId}`)).data
        if(response.code===200){
            roleList.value=response.data;
            addRoleList.value.chooseroleList=roleList.value.userRoleList;
            addRoleList.value.userId=userId
        }else{
            ElMessage.error(`获取失败${userId}`)
        }
    }

    async function saveUserRole() {
        const response=(await request.post(`/user/admin/saveRoleList`,addRoleList.value)).data
        if(response.code===200){
            ElMessage.success("保存成功")
        }else{
            ElMessage.error(`保存失败${response.msg}`)
        }
    }

    async function getUserList() {
        const response= (await request.post(`/user/admin/getUserList`,userParm.value)).data
        if(response.code===200){
            userList.value=response.data
        }else{
            ElMessage.error(`获取用户信息失败${response.msg}`)
        }
    }

    async function delUser(id:number) {
        const response=(await request.delete(`/user/admin/delUser/${id}`)).data
        if(response.code===200){
            ElMessage.success('删除用户成功')
            getUserList()
        }else{
            ElMessage.error(`删除失败,${response.msg}`)
        }
    }

    function handlePageChange(newPage:number){
        userParm.value.pageNo=newPage
        getUserList()
    }



    return {userParm,userList,roleList,addRoleList,disableParm,diasbleUser,getUserRoleList,saveUserRole,delUser,handlePageChange,getUserList}
    
}